$(document).ready(function() {
    SB.Comment = Backbone.Model.extend({});

    SB.CommentCollection = Backbone.Collection.extend({
        model : SB.Comment
    });
    SB.commentCollection = new SB.CommentCollection();

    SB.CommentView = Backbone.View.extend({
        template : _.template($('#comment-tpl').html()),

        render : function() {
            $(this.el).html(this.template(this.model.toJSON()));
            return this;
        }
    });

    SB.CommentCollectionView = Backbone.View.extend({
        initialize : function() {
            _.bindAll(this, 'render', 'addOne', 'addAll');
            this.collection.on('add', this.addOne, this);
            this.collection.on('reset', this.addAll, this);
            console.log("initialize");
            console.log(this.collection);
        },

        render : function() {
            $(this.el).html("");
            return this;
        },

        addOne : function(model) {
            var commentView = new SB.CommentView({
                model : model
            });

            $(this.el).append(commentView.render().el);
            console.log(commentView.el);
            console.log(this.el);
            
        },

        addAll : function() {
            var $this = this;
            this.render();
            $.each(this.collection.models, function(index, model){
            	console.log(model);
            	console.log(model.toJSON());
            	console.log("addAll after reset");
            	
                $this.addOne(model);
            });
        }
    });
});
